package com.john.booter.module.role.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.john.booter.domain.entity.Option;
import com.john.booter.domain.entity.Role;
import com.john.booter.module.role.dao.RoleDao;
import com.john.booter.pagination.DataTable;
import com.john.booter.pagination.Pager;

@Service
public class RoleDaoImpl implements RoleDao {
	
	@PersistenceContext
	private EntityManager em;

	@Override
	public void add(Role r) {
		em.persist(r);
	}

	@Override
	public void delete(String code) {
		Query query = em.createQuery("delete from Role t where t.code = ?1");
		query.setParameter(1, code);
		query.executeUpdate();
	}
	
	@Override
	public void batchDelete(List<String> codes) {
		if(CollectionUtils.isEmpty(codes)) {
			return;
		}
		Query query = em.createQuery("delete from Role t where t.code in ?1");
		query.setParameter(1, codes);
		query.executeUpdate();
	}
	
	@Override
	public void update(Role r) {
		em.merge(r);
	}

	@Override
	public List<Role> findAll() {
		return em.createQuery("from Role t", Role.class).getResultList();
	}

	@Override
	public Role findByCode(String code) {
		TypedQuery<Role> query = em.createQuery("from Role t where t.code = ?1", Role.class);
		query.setParameter(1, code);
		List<Role> list = query.getResultList();
		if(CollectionUtils.isNotEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public DataTable<Role> findDataTable(Pager pg, String name) {
		// 该数组顺序按table列顺序排列
		String orderby = (new String[] { "name", "code", "state", "id" })[pg.getOrderColumn()];
		String searchValue = pg.getSearchValue();
		Integer firstResult = pg.getStart();
		Integer pageSize = pg.getPageSize();

		// 组装查询语句
		StringBuilder querySql = new StringBuilder("select t from Role t");
		StringBuilder countSql = new StringBuilder("select count(t.code) from Role t");
		StringBuilder whereSql = new StringBuilder(" where 1=1");
		String orderBy = " order by t." + orderby + " " + pg.getOrderDir();

		// 查询条件
		if (StringUtils.isNotEmpty(searchValue)) {
			whereSql.append(" and t.name like :name");
		}
		querySql.append(whereSql).append(orderBy);

		// 数据查询
		TypedQuery<Role> query = em.createQuery(querySql.toString(), Role.class);
		setParameter(query, searchValue);

		List<Role> data = query.setFirstResult(firstResult).setMaxResults(pageSize).getResultList();
		// 总记录统计
		Query queryTotal = em.createQuery(countSql.toString());
		int recordsTotal = ((Long) queryTotal.getSingleResult()).intValue();

		// 条件查询记录统计
		countSql.append(whereSql);
		Query queryFilter = em.createQuery(countSql.toString());
		setParameter(queryFilter, searchValue);
		int recordsFiltered = ((Long) queryFilter.getSingleResult()).intValue();

		// 分页封装
		DataTable<Role> dataTable = new DataTable<Role>();
		dataTable.setRecordsTotal(recordsTotal);
		dataTable.setRecordsFiltered(recordsFiltered);
		dataTable.setData(data);
		dataTable.setDraw(pg.getDraw());
		return dataTable;
	}
	
	/**拼接查询参数*/
	private void setParameter(Query query, String cnd) {
		if (StringUtils.isNotEmpty(cnd)) {
			query.setParameter("name", "%" + cnd + "%");
		}
	}

}
